package controller;
import java.io.IOException;
import java.util.UUID;

import dao.FixDao;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet("/FixServlet")
public class FixServlet extends HttpServlet{
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		  // 获取或创建 session
        HttpSession session = request.getSession();
 
        // 从 session 中获取 id，
        
       		
		String building = request.getParameter("building");
        String floor = request.getParameter("floor");
        String room = request.getParameter("room");
        String appointmentDate = request.getParameter("appointmentDate");
        String timeSlot = request.getParameter("timeSlot");
        String description = request.getParameter("description");
        
 
        // 添加一个不会重名的表单号
        String Fix_id = UUID.randomUUID().toString();
 
        // Create an instance of FixDao
        FixDao fixDao = new FixDao();
 
        try {
        if(room!=null) {
        	String id = (String) session.getAttribute("student");
        	fixDao.addFixStu(Fix_id,id ,building, floor , room, appointmentDate + " " + timeSlot, description);
            response.getWriter().println("提交成功");
            response.sendRedirect("View_fix/StudentFix.jsp");
        }else {
        	String id = (String) session.getAttribute("sg");
        	fixDao.addFixSg(Fix_id, id,building, floor , appointmentDate + " " + timeSlot, description);
            response.getWriter().println("提交成功");
   		 response.sendRedirect("View_fix/SgFix.jsp");
        }
        		 
        	
           
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            response.getWriter().println("Error: " + e.getMessage());
        }
    }
	
}

